package ext.mango.functions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import mango.Mango;
import mango.functions.FuncString;
import mango.orm.DaoFactory;
import mango.service.ServiceManager;

import ext.mango.aa.eo.EOAAUser;
import ext.mango.aa.so.AppManager;
import ext.mango.base.Const;
import ext.mango.base.Messages;

public final class Func {
    public static final String welcome(EOAAUser user) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH"); //$NON-NLS-1$
        int now = Integer.parseInt(sdf.format(new Date()));
        StringBuilder ret = new StringBuilder();
        ret.append("<span class='welcome-text'>"); //$NON-NLS-1$
        if (now >= 5 && now <= 9) {
            ret.append(Messages.getString(user.getLanguage(),"goodmoning")); //$NON-NLS-1$
        }
        else if (now > 9 && now < 12) {
            ret.append(Messages.getString(user.getLanguage(),"goodam")); //$NON-NLS-1$
        }
        else if (now >= 12 && now < 14) {
            ret.append(Messages.getString(user.getLanguage(),"goodmiddle")); //$NON-NLS-1$
        }
        else if (now >=14 && now < 18 ) {
            ret.append(Messages.getString(user.getLanguage(),"goodafternoon")); //$NON-NLS-1$
        }
        else if (now >= 18 && now < 22) {
            ret.append(Messages.getString(user.getLanguage(),"goodeve")); //$NON-NLS-1$
        }
        else {
            ret.append(Messages.getString(user.getLanguage(),"nightdeep")); //$NON-NLS-1$
        }
        ret.append("</span>"); //$NON-NLS-1$
        ret.append("<span class='user-name-text'>").append(user.getUsrName()).append("</span>"); //$NON-NLS-1$ //$NON-NLS-2$
        return ret.toString();
    }
    
    public static final String themeOption(EOAAUser user){
        String sql = "select code from BaseCode where codetype='extthemes'"; //$NON-NLS-1$
        List<String> themes = DaoFactory.c(sql).listQueryString();
        StringBuilder ret = new StringBuilder();
        ret.append("["); //$NON-NLS-1$
        for(String theme : themes){
            ret.append("{text:'").append(theme).append("',handler:changeTheme"); //$NON-NLS-1$ //$NON-NLS-2$
            if(( FuncString.isBlank(user.getTheme()) && "Gray".equals(theme))|| theme.equals(user.getTheme())) { //$NON-NLS-1$
                ret.append(",disabled:true"); //$NON-NLS-1$
            }
            ret.append("},"); //$NON-NLS-1$
        }
        if(themes.size() > 0){
            ret.delete(ret.length()-1, ret.length());
        }
        ret.append("]"); //$NON-NLS-1$
        return ret.toString();
    }
    
    public static final String getThemeStyle(EOAAUser user){
        if(FuncString.isBlank(user.getTheme())){
           return "ext-all-gray.css";  //$NON-NLS-1$
        }
        String sql = "select codename from BaseCode where codetype='extthemes' and code=?"; //$NON-NLS-1$
        return DaoFactory.c(sql).setParameter(user.getTheme()).getStringValue();
    }
    
    public static final String getSequenceNo(String prefix) {
        String sql = "select FUNC_AA_GETSEQUENCENO(?, 12) from dual";
        return DaoFactory.c(sql).setParameter(prefix).getStringValue();
    }

    public static final String getUUID(String prefix){
        return FuncString.null2Blank(prefix) + "-" + UUID.randomUUID().toString().toUpperCase(); //$NON-NLS-1$
    }
    
    public static final String getCodeName(String codetype, String code) {
        String sql = "select codename from basecode where codetype=? and code=?"; //$NON-NLS-1$
        return DaoFactory.c(sql).setParameter(codetype, code).getStringValue();
    }
    
    public static final String getLanguage(HttpServletRequest request) {
        String lang = null;
        EOAAUser user = (EOAAUser) request.getSession().getAttribute(EOAAUser.class.getName());
        if (user != null) {
            lang = user.getLanguage();
            if(!FuncString.isBlank(lang)) {
                return lang;
            }
        }
        lang = request.getHeader("accept-language");
        lang = lang.replaceAll("-", "_").toUpperCase();
        if(lang.indexOf(Const.LANG_CN.toUpperCase()) >= 0) {
            lang = Const.LANG_CN;
        }
        else if(lang.indexOf(Const.LANG_EN.toUpperCase()) >= 0) {
            lang =  Const.LANG_EN;
        }
        else {
            lang = Const.LANG_CN;
        }
        if(user != null) {
            user.setLanguage(lang);
            user.update();
        }
        return lang;
    }
    
    public static final String getAppName(HttpServletRequest request) {
        return ServiceManager.getService(AppManager.class).getAppName(getLanguage(request));
    }
    
    public static final String getSpendTime(long start) {
        long end = System.currentTimeMillis();
        long dt = (end - start) / 1000;
        long h = dt / 3600;
        long m = (dt - h * 3600) / 60;
        long s = (dt - h * 3600 - m * 60);
        return h + " hour " + m + " minute " + s + "second";
    }
    
    public static void main(String[] args) {
        Mango.initialize();
        System.out.println(Func.getSequenceNo("28"));
    }
}
